/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.ui.forms.DkFixedFormPanel;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.propertyaccess.SupplierDetailPropertyAccess;
import com.daikit.daikit4gxtsample.client.screen.testscreen.TestScreen;
import com.daikit.daikit4gxtsample.client.ui.field.SupplierDetailEditorGrid;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.google.gwt.editor.client.Editor;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.form.FieldLabel;


/**
 * Second tab of {@link TestScreen} editor
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class ProductEditorTab4 extends AbstractEditorTab implements Editor<ProductData>
{

	private final SupplierDetailEditorGrid supplierDetailsGrid;
	private final FieldLabel labelSupplierDetail;

	/**
	 * Constructor
	 * 
	 * @param controller
	 */
	public ProductEditorTab4(final ProductEditorController controller)
	{
		super(controller);
		final TabItemConfig tabItemConfig = new TabItemConfig();
		tabItemConfig.setIcon(Main.icons().table_16());
		tabItemConfig.setText(Main.gwtSampleI18n().test_screen_editor_tab_4_title());
		setTabItemConfig(tabItemConfig);

		supplierDetailsGrid = new SupplierDetailEditorGrid(new ListStore<SupplierDetailData>(
				SupplierDetailPropertyAccess.INSTANCE.key()))
		//		supplierDetailsGrid = new SupplierDetailEditorGrid(getEditor().supplierDetails.getStore())
		{
			@Override
			public void invalidateUi()
			{
				setEnabled(Main.model().isLogged() && getEditor().isEditing());
			}
		};

		final DkFixedFormPanel myFormPanel = new DkFixedFormPanel(2);

		labelSupplierDetail = new FieldLabel(supplierDetailsGrid, "test visibility");

		myFormPanel.add(labelSupplierDetail);

		add(myFormPanel);
	}

	private final ProductData productEdited = null;
	private final boolean oneOver2 = false;

	@Override
	public void invalidateUi()
	{

		getTabItemConfig().setEnabled(getEditor().isEditing());


		//		final boolean productEditedChanged = !DkObjectUtils.equalsWithNull(productEdited, getEditor().getEditedModel());
		//		productEdited = getEditor().getEditedModel();
		//		if (productEditedChanged)
		//		{
		//			oneOver2 = !oneOver2;
		//		}

		//		supplierDetailsGrid.getStore().add(new SupplierDetailData());
		//		labelSupplierDetail.setVisible(Main.model().isLogged() && getEditor().isEditing() && oneOver2);//&& getController().isTab4Visible());

		//		if (getController().isTab4Visible())
		//		{
		//			supplierDetailsGrid.getStore().clear();
		//		}
		if (getController().isTab4Visible())
		{
			final int storeSize = supplierDetailsGrid.getStore().size();
			for (int i = 0; i < storeSize; i++)
			{
				supplierDetailsGrid.getStore().remove(0);
			}
			for (int i = 0; i < 15; i++)
			{
				supplierDetailsGrid.getStore().add(new SupplierDetailData());
			}
		}
		labelSupplierDetail.setVisible(Main.model().isLogged() && getEditor().isEditing() && getController().isTab4Visible());
		//		if (getController().isTab4Visible())
		//		{
		//			supplierDetailsGrid.getStore().clear();
		//			for (int i = 0; i < 15; i++)
		//			{
		//				supplierDetailsGrid.getStore().add(new SupplierDetailData());
		//			}
		//		}
		getController().getProductEditorUI().getTabPanel().forceLayout();
	}

	@Override
	public boolean needToBeReloaded()
	{
		return true;
	}

	@Override
	public DkBaseAction<?> getReloadSubPanelAction(final boolean force, final Object... optionalArgs)
	{
		return null;
	}
}
